<?php
namespace app\admin\controller\article;

use app\admin\business\article\Post as PostBis;
use app\common\lib\Show;
use think\facade\Request;
use think\facade\View;
use app\admin\model\article\Post as PostModel;
use app\admin\validate\article\Post as PostValidate;
class Post extends PostBis
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    /**
     * 列表
     */
    public function index()
    {
        if (Request::isAjax()) {
            $limit=Request::get('limit');
            //按更新时间查找
            $create_time_start = input("get.create_time-start");
            $create_time_end = input("get.create_time-end");
            //按创建时间查找
            $update_time_start = input("get.update_time-start");
            $update_time_end = input("get.update_time-end");
            $whereData=['create_time_start'=>$create_time_start,'create_time_end'=>$create_time_end,'update_time_start'=>$update_time_start,'update_time_end'=>$update_time_end];
            $list = $this->getPostList($whereData,$limit);
            return Show::success($list->items(), ['count' => $list->total(), 'limit' => intval($limit)]);
        }
        return View::fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if (Request::isAjax()) {
            $data = Request::post();
            $id=$this->setPostData($data);
            if(!$id){
                return Show::error($this->msg);  
            }
            return Show::success(['id'=>$id]);
        }
        return View::fetch();
    }

    /**
     * 编辑
     */
    public function edit($id)
    {
        if(!$id&&is_numeric($id)){
            return Show::error('参数错误');
        }
        if (Request::isAjax()) {
            $data = Request::post();
            $data['id'] =$id;
            $data['update_time']=time();
            $result=$this->upPostData($data);
            if(!$result){
                return Show::error($this->msg);
            }
            return Show::success([],[],'更新成功');
        }
        $article =$this->getPostFind($id);
        return View::fetch('', [
            'data' => $article
        ]);
    }

    /**
     * 删除
     */
    public function del($id)
    {
        $article = PostModel::find($id);
        if ($article) {
            try {
                $article->delete();
            } catch (\Exception $e) {
                $this->jsonApi('删除失败', 201, $e->getMessage());
            }
            $this->jsonApi('删除成功');
        }
    }

    /**
     * 选中删除
     */
    public function delall()
    {
        $ids = Request::param('ids');
        if (!is_array($ids)) {
            $this->jsonApi('参数错误', 201);
        }
        try {
            PostModel::destroy($ids);
        } catch (\Exception $e) {
            $this->jsonApi('删除失败', 201, $e->getMessage());
        }
        $this->jsonApi('删除成功');
    }


    /**
     * 回收站
     */
    public function recycle()
    {
        if (Request::isAjax()) {
            if (Request::isPost()) {
                $ids = Request::param('ids');
                if (!is_array($ids)) {
                    $this->jsonApi('参数错误', 201);
                }
                try {
                    if (Request::param('type') == '1') {
                        $data = PostModel::onlyTrashed()->whereIn('id', $ids)->select();
                        foreach ($data as $k) {
                            $k->restore();
                        }
                    } else {
                        PostModel::destroy($ids, true);
                    }
                } catch (\Exception $e) {
                    $this->jsonApi('操作失败', 201, $e->getMessage());
                }
                $this->jsonApi('操作成功');
            }
            $where = [];

            //按更新时间查找
            $start = input("get.create_time-start");
            $end = input("get.create_time-end");
            if ($start && $end) {
                $where[] = ["create_time", "between", [$start, date("Y-m-d", strtotime("$end +1 day"))]];
            }
            //按创建时间查找
            $start = input("get.update_time-start");
            $end = input("get.update_time-end");
            if ($start && $end) {
                $where[] = ["update_time", "between", [$start, date("Y-m-d", strtotime("$end +1 day"))]];
            }
            $list = PostModel::onlyTrashed()->order('id', 'desc')->where($where)->paginate(Request::get('limit'));

            //重整数组
            foreach ($list as $k => $v) {
            }
            $this->jsonApi('', 0, $list->items(), ['count' => $list->total(), 'limit' => Request::get('limit')]);
        }
        return View::fetch();
    }
}
